package ar.edu.itba.it.paw.web.user;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.domain.exceptions.RegisteredMailException;
import ar.edu.itba.it.paw.domain.exceptions.RegisteredUsernameException;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.base.BasePage;

public class RegisterUserPage extends BasePage {

	@SpringBean
	private UserRepo userRepo;
	private TextField<String> username_field;
	private PasswordTextField password_field;
	private PasswordTextField confirmation_password_field;
	private TextField<String> first_name_field;
	private TextField<String> last_name_field;
	private TextField<String> mail_field;
	private TextField<String> secret_question_field;
	private TextField<String> secret_answer_field;
	
	private transient String username;
	private transient String password;
	private transient String confirmation_password;
	private transient String first_name;
	private transient String last_name;
	private transient String mail;
	private transient String secret_question;
	private transient String secret_answer;
	
	public RegisterUserPage(){
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<RegisterUserPage> form = new Form<RegisterUserPage>("registerForm", new CompoundPropertyModel<RegisterUserPage>(this)) {
			@Override
			protected void onSubmit() {
				try{
					userRepo.addUser(new User(username,password,first_name,last_name,mail,secret_question,secret_answer));
				}catch(RegisteredUsernameException e){
					error(getString("registeredUser"));
					return;
				}catch(RegisteredMailException e){
					error(getString("registeredMail"));
					return;
				}
				setResponsePage(getApplication().getHomePage());			
				}
		};
		first_name_field = new TextField<String>("first_name", new PropertyModel<String>(this,"first_name"));
		first_name_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(first_name_field);
		last_name_field = new TextField<String>("last_name", new PropertyModel<String>(this,"last_name"));
		last_name_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(last_name_field);
		username_field = new TextField<String>("username", new PropertyModel<String>(this,"username"));
		username_field.setRequired(true).add(StringValidator.maximumLength(20));
		form.add(username_field);
		password_field = new PasswordTextField("password",new PropertyModel<String>(this,"password"));
		password_field.setRequired(true).add(StringValidator.minimumLength(8));
		form.add(password_field);
		confirmation_password_field = new PasswordTextField("confirmation_password", new PropertyModel<String>(this,"confirmation_password"));
		confirmation_password_field.add(StringValidator.minimumLength(8));
		form.add(confirmation_password_field);
		mail_field = new TextField<String>("mail", new PropertyModel<String>(this,"mail"));
		mail_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(mail_field);
		secret_question_field = new TextField<String>("secret_question", new PropertyModel<String>(this,"secret_question"));
		secret_question_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(secret_question_field);
		secret_answer_field = new TextField<String>("secret_answer", new PropertyModel<String>(this,"secret_answer"));
		secret_answer_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(secret_answer_field);
		form.add(new Button("register", new ResourceModel("register")));
		//form.add(new Button("clean", new ResourceModel("clean")));
		form.add(new AbstractFormValidator() {
			
			@Override
			public void validate(Form<?> form) {
				if(!password_field.getInput().equals(confirmation_password_field.getInput())){
					form.error(getString("passwordMatch"));
					return;
					
				}
				
			}
			
			@Override
			public FormComponent<?>[] getDependentFormComponents() {
				return new FormComponent[] {first_name_field,last_name_field,secret_question_field,secret_answer_field,username_field,password_field,confirmation_password_field,mail_field};

			}
		});
		add(form);
	}
	
}
